var jira_loader = null;
var jira = {
		serverUrl:null,
		resolutinos: null,
		issuetypes: null,
		priorities: null,
		statuses: null,
		init: function(){
			try{
				jira.serverUrl = JiraLocalStorage.getItem("url");
				jira.resolutions = jira_loader.resolutions;
				jira.issuetypes = jira_loader.issuetypes;
				jira.priorities = jira_loader.priorities;
				jira.statuses = jira_loader.statuses;
				if(jira.serverUrl)
				{
					jira.initHeaderLinks();
					if(JiraLocalStorage.getItem('error')!="")
					{
						jira.error(JiraLocalStorage.getItem('error'));
					 } else {
						try{
								jira.addTab("assignedtome", "Assigned to me");
								if(typeof(jira_loader.issuesFromFilter["assignedtome"]) == "string") {
									$("#table_assignedtome").append(
										$("<tr />").append($("<td />").text(jira_loader.issuesFromFilter["assignedtome"]))
									);
								} else {
									jira.renderTableFromXml("assignedtome");
								}
							jira.getIssuesFromFilter();
						}catch(e){
							jira.error(e);
						}
					}
				} else {
					jira.error('Configure first!');
				}
			}catch(e){
				jira.error(e);
			}
		},
		getIssuesFromFilter: function(){
			var filters = jira_loader.filters;
			$.map(filters, function(item, i){
					jira.addTab(item.id, item.name);
			});
			jira.tabs();
			$.map(filters, function(item, i){
					jira.renderTableFromXml(item.id);
		
			});
		},
		renderTableFromXml: function(id){
			try{
				if(typeof(jira_loader.issuesFromFilter[id]) != 'undefined'){
					$("#table_"+id).dataTable( {
					"bJQueryUI": false,
					"aaData": jira_loader.issuesFromFilter[id],
					"aaSorting": [[ 5, "asc" ]],
					"aoColumns": [
							{ "sTitle": "", "sClass": "Icon",  "fnRender": function(obj) { return (jira.issuetypes[obj.aData[ obj.iDataColumn ]])?("<img title=\""+ jira.issuetypes[obj.aData[ obj.iDataColumn ]].text +"\" src='" + jira.issuetypes[obj.aData[ obj.iDataColumn ]].icon +"'>"):"";}},
							{ "sTitle": "Key",  "fnRender": function(obj) { return "<a target='_main' href=\""+jira.url("/browse/"+ obj.aData[ obj.iDataColumn ])+"\">"+obj.aData[ obj.iDataColumn ]+"</a>" ;}},
							{ "sTitle": "Summary", "sClass": "Summary"},
							{ "sTitle": "Assignee",  "fnRender": function(obj) { if(obj.aData[ obj.iDataColumn ] && obj.aData[ obj.iDataColumn ].length>10)return obj.aData[ obj.iDataColumn ].substr(0, 10)+"..."; else return obj.aData[ obj.iDataColumn ];}},
							{ "sTitle": "Due date", "sClass": "Date"},
							//{ "sTitle": "Est.", "sClass": "Date"},
							{ "sTitle": "", "sClass": "Icon",  "fnRender": function(obj) { return (jira.priorities[obj.aData[ obj.iDataColumn ]])?("<img title=\""+ jira.priorities[obj.aData[ obj.iDataColumn ]].text +"\" src='" + jira.priorities[obj.aData[ obj.iDataColumn ]].icon+"'>"):"";}},
							{"sTitle": "Res.", "sClass": "ShortField"},
							{ "sTitle": "", "sClass": "Icon",  "fnRender": function(obj) { return (jira.statuses[obj.aData[ obj.iDataColumn ]])?("<img title=\""+ jira.statuses[obj.aData[ obj.iDataColumn ]].text +"\" src='" + jira.statuses[obj.aData[ obj.iDataColumn ]].icon+"'>"):"";}},
						]
					} );
				}
			}catch(e){
				jira.error(e);
			}
		},
		addTab: function(id, name){
			$("#tabHeader").append(
				$("<LI />").append(
					$("<A />").attr("href", "#div_"+id).text(name +
							((typeof(jira_loader.issuesFromFilter[id]) != "string")?
									("(" + jira_loader.issuesFromFilter[id].length + ")"):''))
				)
			);
			$("#tabs").append(
				$("<DIV />").attr("id", "div_"+id).append(
					$("<TABLE />").attr("id", "table_"+id).addClass("display")
				).append($("<BR />"))
			);
		},
		getXml: function(name, callback){
				var sXml = JiraLocalStorage.getItem(name);
				var data = (new DOMParser()).parseFromString(sXml, "text/xml");
				callback(data);
		},
		error: function(err){
			JiraLocalStorage.setItem("error",err);
			window.location.href = 'chrome://jira/content/options.html';
			/*
			$("body").append($("<DIV />").addClass("error").text(err)).
				append($("<HR />")).
				append($("<h1 />").append($("<a />").attr("href", 'chrome://jira/content/options.html').attr("target", "_main").text("Options")));
			$("#tabs").hide();
			*/
		}, 
		tabs: function(){
						$('#tabs').tabs({
						select: function(event, ui) {
							JiraLocalStorage.setItem('lastOpenedTab', ui.index);
						},
						selected: (JiraLocalStorage.getItem('lastOpenedTab')?JiraLocalStorage.getItem('lastOpenedTab'):0)
					});
		},
		initHeaderLinks: function(){
				$("#HeaderLink").append(
					$("<a />").addClass("HeaderLink").attr("href", jira.url('/secure/ManageFilters.jspa')).attr("target", "_main").text("Manage Filters")
				).append(
					$("<a />").addClass("HeaderLink").attr("href", 'chrome://jira/content/options.html').text("Options")
				).append(
					$("<a />").addClass("HeaderLink").attr("href", "https://addons.mozilla.org/ru/firefox/addon/146346").attr("target", "_main").text("Contribute")
				);
		},
		url: function(str){
			return (jira.serverUrl + str);
		}
}

var height = 0;
function JiraInit(tool, wnd){
	jira_loader = tool;
	jira.init();
	
	var timer = Components.classes["@mozilla.org/timer;1"]
                    .createInstance(Components.interfaces.nsITimer);
	timer.initWithCallback(function(){
			if($("#tabs").height() != height){
				height = $("#tabs").height();
				wnd.resizeTo(750, height+50);
			}
		},500,Components.interfaces.nsITimer.TYPE_REPEATING_PRECISE);
}